package algorithms;

import java.util.HashMap;

import dataStructure.INode;

public abstract class Algo {

	String filePathFormat;
	int fileStartIndex;
	int fileEndIndex;

	int numOfItems;
	int numOfBaskets;

	/***
	 * Precondition - all the baskets are placed in several files (one basket
	 * can be in one file. One file can contain many baskets). According to the
	 * path from start to end. Comparison is case insensitive!
	 */
	public Algo(String _filePathFormat, int _fileStartIndex, int _fileEndIndex) {
		filePathFormat = _filePathFormat;
		fileStartIndex = _fileStartIndex;
		fileEndIndex = _fileEndIndex;
	}

	/***
	 * Return the most frequent itemsets which are more frequent than the
	 * support and has more items (of type T) than minimumK.
	 * 
	 * @param support
	 *            - min frequency to consider items set frequent.
	 * @param minimumK
	 *            - min size of itemset to consider it interesting.
	 * @return Map - for each K we keep the frequent K-itemsets.
	 */
	public abstract INode findFrequentItemSet(double support,
			int minimumK, int maxK);

	public int getNumOfItems() {
		return numOfItems;
	}

	public int getNumOfBaskets() {
		return numOfBaskets;
	}

	// STATS:
	private static class StatsHolder {
		int count;
	}

	private static HashMap<Integer, StatsHolder> levelToApperneces = new HashMap<Integer, StatsHolder>();
	private static int maxK = 0;
	public static int iterNum = 0;

	public static synchronized void incStats(int k) {
		if (levelToApperneces.containsKey(k)) {
			levelToApperneces.get(k).count++;
		} else {
			if (k > maxK)
				maxK = k;
			StatsHolder tmp = new StatsHolder();
			tmp.count = 1;
			levelToApperneces.put(k, tmp);
		}
	}

	public static String getStats() {
		StringBuilder sb = new StringBuilder();

		for (int i = 1; i <= maxK; i++) {
			sb.append(String.format(" level - %d@items - %d\n", i,
					levelToApperneces.get(i).count));
		}

		return sb.toString();
	}
}
